//课程列表
app.controller('CourseList', function($scope, $location, $http, common){
	$scope.list 	= {};
	$scope.nPage 	= 1;
	$scope.pageHtml = '';
	$scope.aItem 	= {'buy_type' : 1, 'cate' : 1};
	$scope.nItem	= {};
	$scope.teachers = {};
	$scope.dateS 	= '';
	$scope.dateE 	= '';
	$scope.loading 	= false;

	//初始化JS
	$scope.init_js 	= function(){
		$('#upload-add, #upload-edit').uploadifive({
			'auto'             	: true,
			'uploadScript'     	: config.prefix_url+'/Public/upload',
			'removeCompleted'	: true,
			'onUploadComplete' 	: function(file, info) {
				var serverDir 	= 'http://'+window.location.host+'/server';
				var info 	= eval('('+info+')');
				var domId 	= $(this).attr('id');
				console.log(info);
				$scope.$apply(function(){
					switch(domId){
						case 'upload-add':
							$scope.aItem.banner 	= serverDir + info.data.info.path;
						break;
						case 'upload-edit':
							$scope.nItem.banner 	= serverDir + info.data.info.path;
						break;
					}
				})
			}
		});

		$('.input-date').datetimepicker({
	        language:  'zh-CN',
	        format: 'yyyy-mm-dd',
	        weekStart: 1,
	        todayBtn:  1,
	        autoclose: 1,
	        todayHighlight: 1,
	        startView: 2,
	        forceParse: 0,
	        showMeridian: 1,
	        minView	: 2
	    });
	}

	//加载数据
	$scope.load_data 	= function(page){
		if(!page)	page = 1;
		var url 	= config.prefix_url+'/Course/getList?p='+page;
		var param 	= '&date_start='+$scope.dateS+'&date_end='+$scope.dateE;
		
		$http.get(url+param).success(function(info){
			console.log(info);
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
			}else{
				$scope.nPage 	= page;
				$scope.list 	= info.data.list;
				$scope.pageHtml	= info.data.page_html;
				$scope.teachers = info.data.teachers;
				common.startPage($scope.load_data);
			}
		});
		$scope.loading 	= true;
	}

	//添加课程
	$scope.add_item 	= function(info){
		var url 	= config.prefix_url+'/Course/addCourse?';
		var param 	= info;
		$http.post(url, param).success(function(info){
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
			}else{
				$scope.load_data($scope.nPage);
			}
			$('.modal.add-course').modal('hide');
		});
		$scope.loading 	= true;
	}

	//编辑课程
	$scope.get_item 	= function(item){
		console.log(item);
		$scope.nItem 	= item;
		$('.modal.edit-course').modal('show');
	}

	//保存课程
	$scope.update_item 	= function(id, info){
		var url 	= config.prefix_url+'/Course/changeCourse';
		var param 	= {
			'course_id'	: id,
			'course_info'	: info
		}
		console.log(param);
		$http.post(url, param).success(function(info){
			console.log(info);
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			$scope.load_data($scope.nPage);
		});
		$scope.loading 	= true;
		$('.modal.edit-course').modal('hide');
	}

	//进入章节
	$scope.into_chapter 	= function(courseId){
		for(var k in $scope.list){
			if($scope.list[k].id == courseId)
				S('select_course_info', $scope.list[k]);
		}
		$location.path('/Chapter/list');
	}

	$scope.load_data(1);
	$scope.init_js();
});

//章节列表
app.controller('ChapterList', function($scope, $location, $http, common){
	$scope.courseInfo 	= S('select_course_info');
	if(!$scope.courseInfo){
		common.doAlert('无效课程信息!');
		return false;
	}
	$scope.list 	= {};
	$scope.aChapter 	= {imgs : new Array(), course_id : $scope.courseInfo.id, is_try: 0};
	$scope.loading 	= false;

	//初始化JS
	$scope.init_js 	= function(){
		$('#upload-add, #upload-mp3, #upload-edit, #upload-edit-mp3').uploadifive({
			'auto'             	: true,
			'uploadScript'     	: config.prefix_url+'/Public/upload',
			'removeCompleted'	: true,
			'onUploadComplete' 	: function(file, info) {
				var info 	= eval('('+info+')');
				if(info.code != 1){
					common.dealError(info);
					return false;
				}
				var fileInfo 	= info.data.info;
				var filePath = config.host_name + '/server' + info.data.info.path;
				var fileExt 	= filePath.split('.');
					fileExt 	= fileExt[fileExt.length-1];
					fileExt 	= fileExt.toLowerCase();
					fileInfo.path 	= filePath;
				var domId 	= $(this).attr('id');
				$scope.$apply(function(){
					switch(domId){
						case 'upload-add':
							if(fileExt != 'jpg'){
								common.doAlert('文件格式非法!');
								break;
							}
							$scope.aChapter.imgs.push(fileInfo);
						break;
						case 'upload-edit':
							if(fileExt != 'jpg'){
								common.doAlert('文件格式非法!');
								break;
							}
							$scope.eChapter.imgs.push(fileInfo);
						break;
						case 'upload-mp3':
							if(fileExt != 'mp3'){
								common.doAlert('文件格式非法!');
								break;
							}
							$scope.aChapter.audio 	= fileInfo.path;
						break;
						case 'upload-edit-mp3':
							if(fileExt != 'mp3'){
								common.doAlert('文件格式非法!');
								break;
							}
							$scope.eChapter.audio 	= fileInfo.path;
						break;
					}
				})
			}
		});
	}
	//加载数据
	$scope.load_data 	= function(){
		var url 	= config.prefix_url+'/Course/getChapterList?course_id='+$scope.courseInfo.id;
		$http.get(url).success(function(info){
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
			}else{
				$scope.list 	= info.data.list;
			}
		});
		$scope.loading 	= true;
	}

	//删除图片
	$scope.remove_img 	= function(img, type, dom){
		switch(type){
			case 'add':
				var object 	= $scope.aChapter.imgs;
			break;
			case 'edit':
				var object 	= $scope.eChapter.imgs;
			break;
		}	
		for(var k in object){
			console.log(k);
			if(img == object[k]){
				object.splice(k, 1);
				break;
			}
		}

	}

	//添加章节
	$scope.add 	= function(){
		var url 	= config.prefix_url+'/Course/addChapter?';
		var param 	= $scope.aChapter;
		$http.post(url, param).success(function(info){
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
			}else{
				$scope.load_data();
				$('.modal.chapter-add').modal('hide');
			}
		});
		$scope.loading 	= true;
	}

	//编辑章节
	$scope.edit 	= function(chapterId){
		for(var k in $scope.list){
			if($scope.list[k].id == chapterId){
				$scope.eChapter 	= $scope.list[k];
				break;
			}
		}
		for(var k in $scope.eChapter.imgs){
			$scope.eChapter.imgs[k].path	= config.host_name + $scope.eChapter.imgs[k].path;
		}
		$scope.eChapter.time 	= parseInt($scope.eChapter.time);
		$('.modal.chapter-edit').modal('show');
	}

	//保存编辑
	$scope.save 	= function(){
		var url 	= config.prefix_url+'/Course/changeChapter?';
		var param 	= $scope.eChapter;
			param.chapter_id 	= param.id;

		$http.post(url, param).success(function(info){
			$('.modal.chapter-edit').modal('hide');
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
			}else{
				$scope.load_data();
			}
		})
		$scope.loading 	= true;
	}

	//删除章节
	$scope.delete 	= function(chapterId){
		var url 	= config.prefix_url+'/Course/changeChapter?';
		var param 	= {
			'chapter_id'	: chapterId,
			'status'	: -1
		}
		$http.post(url, param).success(function(info){
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
			}else{
				$scope.load_data();
			}
		})
		$scope.loading 	= true;
	}

	$scope.load_data();
	$scope.init_js();
});

//课程售出记录
app.controller('CourseSellLog', function($scope, $location, $http, common){
	$scope.listA 	= {list: new Array()};
	$scope.listB 	= {list: new Array()};
	$scope.listC 	= {list: new Array()};
	$scope.listD 	= {list: new Array()};
	$scope.listE 	= {list: new Array()};
	$scope.object 	= $scope.listA;
	$scope.keyword 	= '';
	$scope.nPage 	= 1;
	$scope.getType 	= 0;
	$scope.pageHtml 	= '';
	$scope.moneyInfo 	= {};
	$scope.userCInfo 	= {};
	$scope.deductInfos 	= {};
	$scope.dateStart 	= '';
	$scope.dateEnd 	= '';
	$scope.loading 	= false;

	//初始化JS
	$scope.init_js 	= function(){
		$('.input-date').datetimepicker({
	        language:  'zh-CN',
	        format: 'yyyy-mm-dd',
	        weekStart: 1,
	        todayBtn:  1,
	        autoclose: 1,
	        todayHighlight: 1,
	        startView: 2,
	        forceParse: 0,
	        showMeridian: 1,
	        minView	: 2
	    });
		//标签页钩子设置
        $('a[data-toggle="tab"]').on('show.bs.tab', function(e){
            var type    = $(this).data('target');
            switch(type){
                case '#list-all': 
                	$scope.getType 	= 0;
                	$scope.object 	= $scope.listA;
                break;
                case '#list-money' : 
                	$scope.getType 	= 1;
                	$scope.object 	= $scope.listB;
                break;
                case '#list-free' : 
                	$scope.getType 	= 4;
                	$scope.object 	= $scope.listC;
                break;
                case '#list-score': 
                	$scope.getType 	= 5;
                	$scope.object 	= $scope.listD;
                break;
                case '#list-shopcart': 
                	$scope.getType 	= 3;
                	$scope.object 	= $scope.listE;
                break;
                default: return false; break;
            }
            $scope.load_data(1);
        });

        //回车键监听
	    common.listenEnterKey(function(){
	        $scope.load_data(1);
	    });
	}
	//加载数据
	$scope.load_data 	= function(page){
		var url 	= config.prefix_url+'/Course/sellLog?'+
			'&keyword='+$scope.keyword+
			'&get_type='+$scope.getType+
			'&date_start='+$scope.dateStart+
			'&date_end='+$scope.dateEnd+
			'&p='+page;
		
		$http.get(url).success(function(info){
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			$scope.object.list 	= info.data.list;
			$scope.pageHtml 	= info.data.page_html;
			common.startPage($scope.load_data);
		});
		$scope.loading 	= true;
	}

	//详情信息
	$scope.detail 	= function(courseId, getType, getRelateId){
		for(var k in $scope.object.list){
			if($scope.object.list[k].course_id == courseId){
				$scope.userCInfo 	= $scope.object.list[k];
				break;
			}
		}

		var url 	= config.prefix_url+'/Course/detailBargainInfo?'+
			'&get_type='+getType+
			'&get_relate_id='+getRelateId;
		
		$http.get(url).success(function(info){
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			$scope.moneyInfo 	= info.data.money_info;
			$('.modal.userc-detail').modal('show');
		});
		$scope.loading 	= true;
	}

	// //‘加载’样式
 //    $scope.load_style   = function(type){
 //        switch(type){
 //            case 'start':
 //                $('.title').children('.fa-spinner').css('display', 'inline');
 //            break;
 //            case 'stop':
 //                $('.title').children('.fa-spinner').css('display', 'none');
 //            break;
 //        }
 //    }

	$scope.load_data(1);
	$scope.init_js();
});

//课程交易记录
app.controller('CourseBargainList', function($scope,$location, $http, common){
	$scope.list 	= {}
	$scope.loading 	= false;
	$scope.nPage 	= 1;
	$scope.pageHtml 	= '';
	$scope.keyword 	= '';
	$scope.dateStart 	= '';
	$scope.dateEnd 	= '';
	$scope.dataType 	= 1;
	$scope.total 	= {}

	//初始化JS
	$scope.init_js 	= function(){
		$('.input-date').datetimepicker({
	        language:  'zh-CN',
	        format: 'yyyy-mm-dd',
	        weekStart: 1,
	        todayBtn:  1,
	        autoclose: 1,
	        todayHighlight: 1,
	        startView: 2,
	        forceParse: 0,
	        showMeridian: 1,
	        minView	: 2
	    });

	    //切页钩子
	    $('a[data-toggle="tab"]').on('show.bs.tab', function(e){
            var type    = $(this).data('target');
            switch(type){
                case '#list-course': 
                	$scope.dataType 	= 1;
                break;
                case '#list-shopcart' : 
                	$scope.dataType 	= 2;
                break;
                default: return true; break;
            }
            $scope.load_data(1);
        });

        //回车键监听
	    common.listenEnterKey(function(){
	        $scope.load_data(1);
	    });
	}	

	//加载数据
	$scope.load_data 	= function(page){
		var url 	= config.prefix_url + '/Course/getBargainList?'+
			'&p='+page+
			'&keyword='+$scope.keyword+
			'&date_start='+$scope.dateStart+
			'&date_end='+$scope.dateEnd+
			'&data_type='+$scope.dataType;

		$scope.list 	= {};
		$http.get(url).success(function(info){
			$scope.loading 	= false;
			console.log(info);
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			$scope.nPage 	= page;
			$scope.list 	= info.data.list;
			$scope.pageHtml 	= info.data.page_html;
			$scope.total 	= info.data.total;
			common.startPage($scope.load_data);
		});
		$scope.loading 	= true;
	}

	//交易详情
	$scope.detail 	= function(item, getType, getRelateId){
		$scope.nItemInfo 	= item;
		var url 	= config.prefix_url + '/Course/detailBargainInfo?'+
			'&get_type='+getType+
			'&get_relate_id='+getRelateId;

		$http.get(url).success(function(info){
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			$scope.nMoneyInfo 	= info.data.money_info;
			$scope.nDeductList 	= info.data.deduct_list;
			$scope.loading 	= false;
			$('.modal.bargain-detail').modal('show');
		});
		$scope.loading 	= true;
	}

	//导出excel
	$scope.out_excel 	= function(){
		var url 	= config.prefix_url + '/Course/outBargainExcel?'+
			'&keyword='+$scope.keyword+
			'&date_start='+$scope.dateStart+
			'&date_end='+$scope.dateEnd;

		$http.get(url).success(function(info){
			if(info.code != 1){
				common.dealError(info);
				return false;
			}

			var count 	= info.data.count;
			var length 	= 5000;
			var start 	= 0;
			while(start < count){
				var outUrl 	= url + '&start='+start+'&length='+length;
				window.open(outUrl, '_blank');
				start 	+= length;
			}
		})
	}

	$scope.load_data(1);
	$scope.init_js();
});

//课程数据报表
app.controller('CourseReport', function($scope, $location, $http, common){
	//变量声明
	$scope.dateS 	= '';
	$scope.dateE 	= '';
	$scope.loading 	= false;

	//初始化JS
	$scope.init_js 	= function(){
		$('.input-date').datetimepicker({
	        language:  'zh-CN',
	        format: 'yyyy-mm-dd',
	        weekStart: 1,
	        todayBtn:  1,
	        autoclose: 1,
	        todayHighlight: 1,
	        startView: 2,
	        forceParse: 0,
	        showMeridian: 1,
	        minView	: 2
	    });

	    //回车键监听
	    common.listenEnterKey(function(){
	        $scope.load_data();
	    });
	}

	//加载数据
	$scope.load_data 	= function(){
		var url 	= config.prefix_url+'/Report/getCourse?'+
			'&date_start='+$scope.dateS+
			'&date_end='+$scope.dateE;

		$http.get(url).success(function(info){
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			var xAxis 	= info.data.x_axis;
			var dataCount 	= info.data.series.count;
			for(var k in dataCount){
				for(var _k in dataCount[k]['data']){
					dataCount[k]['data'][_k]	= parseInt(dataCount[k]['data'][_k]);
				}
			}
			var dataAmount 	= info.data.series.amount;
			for(var k in dataAmount){
				for(var _k in dataAmount[k]['data']){
					dataAmount[k]['data'][_k]	= parseInt(dataAmount[k]['data'][_k]);
				}
			}
			common.makeChart($('.chart-amount'), '课程销售金额', xAxis, '销售金额(元)', dataAmount);
			common.makeChart($('.chart-count'), '课程销售数量', xAxis, '销售数量(个)', dataCount);
		});
		$scope.loading 	= true;
	}

	$scope.load_data();
	$scope.init_js();
})